package com.lucaswangdev.logindemo.web.controller;

import com.lucaswangdev.logindemo.biz.request.user.UserCreateRequest;
import com.lucaswangdev.logindemo.biz.request.user.UserDeleteRequest;
import com.lucaswangdev.logindemo.biz.request.user.UserQueryRequest;
import com.lucaswangdev.logindemo.biz.request.user.UserUpdateRequest;
import com.lucaswangdev.logindemo.biz.response.common.PagingResponse;
import com.lucaswangdev.logindemo.biz.service.UserService;
import com.lucaswangdev.logindemo.model.convert.UserConverter;
import com.lucaswangdev.logindemo.model.dto.UserDTO;
import com.lucaswangdev.logindemo.web.BaseResult;
import com.lucaswangdev.logindemo.web.vo.UserVO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 用户注册
     * @param request
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public BaseResult<Void> userRegister(@RequestBody UserCreateRequest request){
        Boolean success = userService.create(request);
        if(success == null || !success) {
            return BaseResult.systemFail();
        }
        return BaseResult.success();
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public BaseResult<UserDTO> userLogin(@RequestBody UserCreateRequest userLoginRequest,  HttpServletRequest request){
        UserDTO userDTO = userService.userLogin(userLoginRequest, request);
        if(userDTO == null) {
            return BaseResult.systemFail();
        }
        UserDTO result = new UserDTO();
        result.setId(userDTO.getId());
        result.setEmail(userDTO.getEmail());
        return BaseResult.success(result);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    public BaseResult<UserDTO> userLogout(HttpServletRequest request){
        boolean result = userService.userLogout(request);
        if(!result) {
            return BaseResult.systemFail();
        }
        return BaseResult.success();
    }


    @RequestMapping(value = "/list-page", method = RequestMethod.POST)
    public BaseResult<PagingResponse<UserVO>> listPage(@RequestBody UserQueryRequest request){
        PagingResponse<UserDTO> response = userService.listPage(request);
        if(response == null) {
            return BaseResult.systemFail();
        }
        if(response.isSuccess()){
            List<UserVO> vos = UserConverter.INSTANCE.convertDTOList2VOList(response.getTableData());
            PagingResponse<UserVO> result = PagingResponse.build(vos, response.getPaging());
            return BaseResult.success(result);
        } else {
            return BaseResult.bizFail(response.getErrorMsg());
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Void> update(@RequestBody UserUpdateRequest request){
        Boolean success = userService.modified(request);
        if(success == null || !success) {
            return BaseResult.systemFail();
        }
        return BaseResult.success();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult<Void> delete(@RequestBody UserDeleteRequest request){
        Boolean success = userService.deleteById(request);
        if(success == null || !success) {
            return BaseResult.systemFail();
        }
        return BaseResult.success();
    }
}
